package com.ncloud.system.controller;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.ncloud.common.body.Result;
import com.ncloud.common.utils.ResultUtil;
import com.ncloud.system.service.impl.DictionaryServiceImpl;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

/**
 * Created by gaowei on 2017/7/27.
 */
@RestController
@RequestMapping("/sys/dictionary")
public class DictionaryController {
	@Autowired
	private DictionaryServiceImpl dictionaryServiceImpl;
	@GetMapping(value = "/list")
    public Result<?> getDictionaryInfo(HttpServletRequest request){
    	JSONArray infos = dictionaryServiceImpl.getDictionaryInfo();
		return ResultUtil.success(infos);
    }
    @GetMapping(value = "/listById/{dictionaryId}")
    public Result<?> getDictionaryById(HttpServletRequest request,@PathVariable("dictionaryId") String dictionaryId){
    	JSONArray infos = dictionaryServiceImpl.getDictionaryById(dictionaryId);
		return ResultUtil.success(infos);
    }
    @GetMapping(value = "/regionlist/{pid}")
    public Result<?> getRegionDataById(HttpServletRequest request,@PathVariable("pid") String pid){
    	JSONArray infos = dictionaryServiceImpl.getRegionDataInfo(pid);
		return ResultUtil.success(infos);
    }
    @PostMapping(value = "/add")
    public Result<?> add(HttpServletRequest request, @RequestBody JSONObject json){
    	Object userID = request.getAttribute("userID");
    	//JSONObject params = JSONObject.parseObject(param);
    	json.put("userID", Long.parseLong(userID.toString()));
    	int num = dictionaryServiceImpl.insertDictionary(json);
		return ResultUtil.success(num);
    }
    @PutMapping(value = "/update")
    public Result<?> update(HttpServletRequest request, @RequestBody JSONObject json){
    	Object userID = request.getAttribute("userID");
    	json.put("userID", Long.parseLong(userID.toString()));
    	int num = dictionaryServiceImpl.updateDictionary(json);
		return ResultUtil.success(num);
    }
    @DeleteMapping(value = "/delete/{dictionaryId}")
    public Result<?> delete(HttpServletRequest request, @PathVariable("dictionaryId") String dictionaryId){
    	int num = dictionaryServiceImpl.deleteDictionaryById(dictionaryId);
		return ResultUtil.success(num);
    }
}
